﻿using IHISCETeim.BLL;
using IHISCETeim.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using IHISCETeim.Library;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace IHISCETeim.Web.AgentSys.VipMgr
{
    public partial class VIPBuyPass : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.VIPBuy_审核,Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get { return "VipBuyList"; }

        }


        protected int BuyId
        {
            get
            {
                return GetRequestValue<int>("BuyId", 0);
            }
        }
        private VIPBuyInfo _vipObj = null;
        protected VIPBuyInfo vipObj
        {
            get
            {
                if (_vipObj == null)
                    _vipObj = VIPBuyController.GetList(BuyId);

                return _vipObj;
            }
        }

        private List<VIPBuyDetailInfo> _listDetail = null;
        protected List<VIPBuyDetailInfo> listDetail
        {
            get
            {
                if (_listDetail == null)
                    _listDetail = VIPBuyController.GetAllDetail(BuyId);

                return _listDetail;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "edit")
            {
                add();//
            }
            bind();
        }

        private void bind()
        {
            this.rpDetail.DataSource = listDetail;
            this.rpDetail.DataBind();
        }
        private void add()
        {
            if (!HasPower(IHISCETeim.Web.Permission.VIPBuy_审核) || vipObj.OrderState == "已确认")
            {
                ErrorJson("你没有权限进行此操作");
                return;
            }
            VIPBuyInfo obj = Request.SerializableObject<VIPBuyInfo>();
            obj.UserId = vipObj.UserId;
            obj.AgentName = vipObj.AgentName;
            obj.AgentTotalPoint = obj.TotalPoint;


            char[] caSplit = new char[] { ',' };
            string[] ItemCodes = GetRequestValue<string>("ItemCode", "").Split(caSplit);
            string[] ItemNames = GetRequestValue<string>("ItemName", "").Split(caSplit);
            string[] Prices = GetRequestValue<string>("Price", "").Split(caSplit);
            string[] Quantitys = GetRequestValue<string>("Quantity", "").Split(caSplit);
            string[] DiscPrcnts = GetRequestValue<string>("DiscPrcnt", "").Split(caSplit);
            string[] LineTotals = GetRequestValue<string>("LineTotal", "").Split(caSplit);
            string[] Points = GetRequestValue<string>("Point", "").Split(caSplit);
            string[] IsDiscPieces = GetRequestValue<string>("IsDiscPiece", "").Split(caSplit);

            List<VIPBuyDetailInfo> list = new List<VIPBuyDetailInfo>();
            for (int rowIndex = 0; rowIndex < ItemCodes.Count(); rowIndex++)
            {
                VIPBuyDetailInfo itm = new VIPBuyDetailInfo();
                itm.ItemCode = ItemCodes[rowIndex];
                itm.ItemName = ItemNames[rowIndex];
                itm.Price = Convert.ToDecimal(Prices[rowIndex]);
                itm.Quantity = Convert.ToDecimal(Quantitys[rowIndex]);
                itm.DiscPrcnt = Convert.ToDecimal(DiscPrcnts[rowIndex]);
                itm.LineTotal = Convert.ToDecimal(LineTotals[rowIndex]);
                itm.Point = Convert.ToInt32(Points[rowIndex]);
                itm.IsDiscPiece = Convert.ToBoolean(IsDiscPieces[rowIndex]) ;
                itm.AgentPoint =itm.IsDiscPiece?0: itm.Point;
                list.Add(itm);
            }
            obj.AgentTotalPoint = list.Sum(e=>e.AgentPoint);//经销商积份

            int R = VIPBuyController.UpdatePass(obj, list, base.User.UserType == 2);
            switch (R)
            {
                case 1:
                    SuccessJson("已成功审核VIP配件采购信息,相应积分已累积到对应的客户及经销商中.");
                    break;              
                case 0:
                    ErrorJson("审核VIP配件采购信息失败");
                    break;
            }
        }
    }
}